﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Maydew.Windows.Forms.Controls.FlexiCell;
using System.Drawing.Drawing2D;

namespace Demo
{
    public partial class Form1 : Form
    {
        private CellBorderStyle _borderStyle;
        private bool _hasBorder;
        private Color _backColor;
        private Font _font;

        public Form1()
        {
            InitializeComponent();

            _hasBorder = true;
            _borderStyle = new CellBorderStyle
            {
                Color = Color.Black,
                DashStyle = DashStyle.Solid,
                Width = 1,
                Brush = null,
            };
            _backColor = Color.White;
            _font = SystemFonts.CaptionFont;
        }

        private void butBorderColor_Click(object sender, EventArgs e)
        {
            ColorDialog form = new ColorDialog();
            form.Color = _borderStyle.Color;
            if (form.ShowDialog(this) == DialogResult.Cancel) return;
            _borderStyle.Color = form.Color;
        }

        private void butLineNo_Click(object sender, EventArgs e)
        {
            _hasBorder = false;
        }

        private void butLine1_Click(object sender, EventArgs e)
        {
            _hasBorder = true;
            _borderStyle.DashStyle = DashStyle.Solid;
        }

        private void butLine2_Click(object sender, EventArgs e)
        {
            _hasBorder = true;
            _borderStyle.DashStyle = DashStyle.Dot;
        }

        private void butBorderBottom_Click(object sender, EventArgs e)
        {
            flexiCellControl1.BeginUpdate();
            if (_hasBorder)
            {
                flexiCellControl1.SetBottomBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
            }
            else
            {
                flexiCellControl1.SetBottomBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
            }
            flexiCellControl1.EndUpdate();
        }

        private void butBorderTop_Click(object sender, EventArgs e)
        {
            flexiCellControl1.BeginUpdate();
            if (_hasBorder)
            {
                flexiCellControl1.SetTopBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
            }
            else
            {
                flexiCellControl1.SetTopBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
            }
            flexiCellControl1.EndUpdate();
        }

        private void butBorderLeft_Click(object sender, EventArgs e)
        {
            flexiCellControl1.BeginUpdate();
            if (_hasBorder)
            {
                flexiCellControl1.SetLeftBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
            }
            else
            {
                flexiCellControl1.SetLeftBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
            }
            flexiCellControl1.EndUpdate();
        }

        private void butBorderRight_Click(object sender, EventArgs e)
        {
            flexiCellControl1.BeginUpdate();
            if (_hasBorder)
            {
                flexiCellControl1.SetRightBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
            }
            else
            {
                flexiCellControl1.SetRightBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
            }
            flexiCellControl1.EndUpdate();
        }

        private void butBorderNo_Click(object sender, EventArgs e)
        {
            flexiCellControl1.BeginUpdate();

            flexiCellControl1.SetBottomBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
            flexiCellControl1.SetTopBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
            flexiCellControl1.SetLeftBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
            flexiCellControl1.SetRightBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
            flexiCellControl1.SetHorizontalInnerStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
            flexiCellControl1.SetVerticalInnerStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
            
            flexiCellControl1.EndUpdate();
        }

        private void butBorderAll_Click(object sender, EventArgs e)
        {
            flexiCellControl1.BeginUpdate();
            if (_hasBorder)
            {
                flexiCellControl1.SetBottomBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
                flexiCellControl1.SetTopBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
                flexiCellControl1.SetLeftBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
                flexiCellControl1.SetRightBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
                flexiCellControl1.SetHorizontalInnerStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
                flexiCellControl1.SetVerticalInnerStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
            }
            else
            {
                flexiCellControl1.SetBottomBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
                flexiCellControl1.SetTopBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
                flexiCellControl1.SetLeftBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
                flexiCellControl1.SetRightBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
                flexiCellControl1.SetHorizontalInnerStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
                flexiCellControl1.SetVerticalInnerStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
            }
            flexiCellControl1.EndUpdate();
        }

        private void butBorderOutside_Click(object sender, EventArgs e)
        {
            flexiCellControl1.BeginUpdate();
            if (_hasBorder)
            {
                flexiCellControl1.SetBottomBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
                flexiCellControl1.SetTopBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
                flexiCellControl1.SetLeftBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
                flexiCellControl1.SetRightBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, _borderStyle.Color, _borderStyle.Brush);
            }
            else
            {
                flexiCellControl1.SetBottomBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
                flexiCellControl1.SetTopBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
                flexiCellControl1.SetLeftBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
                flexiCellControl1.SetRightBorderStyle(_borderStyle.DashStyle, _borderStyle.Width, Color.Transparent, _borderStyle.Brush);
            }
            flexiCellControl1.EndUpdate();
        }

        private void butMerge_Click(object sender, EventArgs e)
        {
            flexiCellControl1.MergeCells();
        }

        private void butUnmerge_Click(object sender, EventArgs e)
        {
            flexiCellControl1.UnmergeCells();
        }

        private void butBackColor_Click(object sender, EventArgs e)
        {
            ColorDialog form = new ColorDialog();
            form.Color = _backColor;
            if (form.ShowDialog(this) == DialogResult.Cancel) return;
            _backColor = form.Color;
            flexiCellControl1.SetBackColor(_backColor);
        }

        private void butForeColor_Click(object sender, EventArgs e)
        {
            ColorDialog form = new ColorDialog();
            form.Color = _backColor;
            if (form.ShowDialog(this) == DialogResult.Cancel) return;
            _backColor = form.Color;
            flexiCellControl1.SetForeColor(_backColor);
        }

        private void butFont_Click(object sender, EventArgs e)
        {
            FontDialog form = new FontDialog();
            form.Font = _font;
            if (form.ShowDialog(this) == DialogResult.Cancel) return;
            _font = form.Font;
            flexiCellControl1.SetFont(_font);
        }

        private void butTopLeft_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SetTextAlign(ContentAlignment.TopLeft);
        }

        private void butTopCenter_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SetTextAlign(ContentAlignment.TopCenter);
        }

        private void butTopRight_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SetTextAlign(ContentAlignment.TopRight);
        }

        private void butMiddleLeft_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SetTextAlign(ContentAlignment.MiddleLeft);
        }

        private void butMiddleCenter_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SetTextAlign(ContentAlignment.MiddleCenter);
        }

        private void butMiddleRight_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SetTextAlign(ContentAlignment.MiddleRight);
        }

        private void butBottomLeft_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SetTextAlign(ContentAlignment.BottomLeft);
        }

        private void butBottomCenter_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SetTextAlign(ContentAlignment.BottomCenter);
        }

        private void butBottomRight_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SetTextAlign(ContentAlignment.BottomRight);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SelectedCell.Editor = new PictureCellEditor();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SelectedCell.Format = "$###,###,##0.00";
        }

        private void butFormat_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SetFormat(txtFormat.Text);
        }

        private void butTextEditor_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SetEditor(new TextCellEditor());
        }

        private void butPictureEditor_Click(object sender, EventArgs e)
        {
            flexiCellControl1.SetEditor(new PictureCellEditor());
        }
    }
}
